package ooday04;

public class PolymorphicDemo {

    public static void main(String[] args) {

        //演示向上造型
        /*Dog dog1 = new Dog("lucy",1,"yellow");
        dog1.eat();
        dog1.drink();
        dog1.swim();
        dog1.lookHome();

        Animal dog2 = new Dog("zera",2,"black");
        dog2.eat();
        dog2.drink();

        Swim dog3 = new Dog("bob",3,"red");
        dog3.swim();*/

        Master master = new Master();
        Dog dogz = new Dog("Alice", 1, "black");
        Chick chick = new Chick("bob", 2, "red");
        Fish fish = new Fish("cyley", 3, "blue");
        master.feed(dogz);
        master.feed(chick);
        master.feed(fish);

        Animal[] animals = new Animal[3];
        animals[0] = dogz;
        animals[1] = chick;
        animals[2] = fish;
        for (int i = 0; i < 3; i++) {
            animals[i].eat();

            if (animals[i] instanceof Dog) {
                Dog dog = (Dog) animals[i];
                dog.lookHome();
            }

            if (animals[i] instanceof Chick){
                Chick chick1 = (Chick) animals[i];
                chick1.layEgg();
            }

            if (animals[i] instanceof Swim){
                Swim swim = (Swim) animals[i];
                swim.swim();
            }

        }

        //向下转型
        Animal dog = new Dog("Alice", 1, "yellow");
        Dog dog1 = (Dog) dog;
        Swim dog2 = (Swim) dog;

        if (dog instanceof Fish) {
            Fish dog3 = (Fish) dog;
        }
        System.out.println(dog instanceof Fish);

    }

}
